let ajaxTimes = 0

export const request = (params) => {

  // 检查token值是否过期
  // const newTime = Date.parse(new Date())
  // const expiration = wx.getStorageSync('expiration')
  const token = wx.getStorageSync('token')

  let header = params['header'] ? { ...params.header } : {}
  // if (!params.url.includes('/wxLogin')) {
  // if (!expiration || expiration <= newTime) {
  //   isGuo = true
  //   wx.showModal({
  //     title: '提示',
  //     content: '登录已过期,请重新登录!',
  //     showCancel: false,
  //     success: (res) => {
  //       wx.reLaunch({
  //         url: '/pages/login/login'
  //       })
  //     }
  //   })
  //   return
  // }
  header['token'] = token ? token : ''
  // }

  ajaxTimes++
  if (!params.url.includes('/read0') && !params.url.includes('/usersInfo')) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
  }
  let baseUrl = 'xxxxxx'
  if (params.url.includes('/readMessage') || params.url.includes('/read0')) {
    baseUrl = 'xxxxx'
    console.log(baseUrl)
  } else {
    baseUrl = 'xxxxxx'
  }
  return new Promise((resolve, reject) => {
    const reqUest = wx.request({
      ...params,
      header,
      url: baseUrl + params.url,
      timeout: 120000,
      success: (result) => {
        // isGuo = false
        resolve(result)
      },
      fail: (err) => {
        reqUest.abort()
        reject(err)
        if (err.errMsg == 'request:fail timeout') {
          wx.showModal({
            title: '提示',
            content: '请求超时了!',
            showCancel: false
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '请求出错了!',
            showCancel: false
          })
        }
      },
      complete: () => {
        ajaxTimes--
        if (ajaxTimes === 0) {
          wx.hideLoading()
        }
      }
    })
  })
}
